using System;
using System.IO;
using System.Net;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Security.Cryptography;
using System.Xml.Serialization;
using com.flajaxian.Dime;

namespace com.flajaxian.AWS
{
    /// <summary>
    /// 
    /// </summary>
    public partial class AmazonPutObjectService
    {

        public AccessControlCollection _accessControlList;
        private string _accessKey;
        private string _secretKey;
        private string _bucketName;
        private string _path = String.Empty;

        /// <summary>
        /// 
        /// </summary>
        public AmazonPutObjectService()
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        public AccessControlCollection AccessControlList
        {
            get { return this._accessControlList; }
            set { this._accessControlList = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string AccessKey
        {
            get { return this._accessKey; }
            set { this._accessKey = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string SecretKey
        {
            get { return this._secretKey; }
            set { this._secretKey = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string BucketName
        {
            get { return this._bucketName; }
            set { this._bucketName = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Path
        {
            get { return this._path; }
            set
            {
                if(String.IsNullOrEmpty(value))
                {
                    return;
                }
                this._path = (value.EndsWith("/"))? value : value + "/";
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileStream"></param>
        /// <returns></returns>
        public HttpWebResponse CallService(Stream fileStream, string fileName)
        {
            DateTime timestamp = DateTime.Now;
            timestamp = new DateTime(timestamp.Year, timestamp.Month, timestamp.Day,
                                 timestamp.Hour, timestamp.Minute, timestamp.Second,
                                 timestamp.Millisecond, DateTimeKind.Local);

            string timestampString =
                timestamp.ToUniversalTime().ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z", System.Globalization.CultureInfo.InvariantCulture);

            string signiture = this.makeSignature(timestamp);

            string filePath = this.Path + fileName;

            string stringXml = String.Format(SR.SoapEnvelop,
                 ForXml(this.BucketName),
                 ForXml(filePath),
                 fileStream.Length,
                 this.GetAccessControlListXml(),
                 this.AccessKey,
                 timestamp.ToString("yyyy-MM-dd\\THH:mm:ss.fffzzz", System.Globalization.CultureInfo.InvariantCulture),
                 signiture);

            byte[] xml = Encoding.UTF8.GetBytes(stringXml);


            DimeManager dm = new DimeManager(xml, fileStream);
            return dm.SendRequest();
        }

        private static string ForXml(string str)
        {
            return (str == null) ? 
                        String.Empty : 
                        str.Replace("&", "&amp;").
                            Replace("<", "&lt;").
                            Replace(">", "&gt;").
                            Replace("\"", "&quot;");
        }

        private string GetAccessControlListXml()
        {
            return this.AccessControlList.ToString();
        }

        private string makeSignature(DateTime timestamp)
        {
            string method = "PutObject";
            string canonicalString = "AmazonS3" + method + timestamp.ToUniversalTime().ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z",
                                System.Globalization.CultureInfo.InvariantCulture);
            Encoding ae = new UTF8Encoding();
            HMACSHA1 signature = new HMACSHA1(ae.GetBytes(this.SecretKey));
            return Convert.ToBase64String(
                                        signature.ComputeHash(ae.GetBytes(
                                                        canonicalString.ToCharArray()))
                                               );
        }
   
    }



    

    
    
        
    
        



}
